package com.kkyy.msystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.kkyy.msystem.dao.PageDao;
import com.kkyy.msystem.entity.UsersEntity;
import com.kkyy.msystem.feign.FeignService2;
import com.kkyy.msystem.page.Page;
import com.kkyy.msystem.service.RegistService;
import com.kkyy.msystem.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/msystem")
public class UsersController {
    @Autowired
    UsersService usersService;

    @Autowired
    RegistService registService;

    @Autowired
    PageDao pageDao;

    @Autowired
    FeignService2 feignService2;

    // 获取用户信息
    @RequestMapping("/userslist")
    public String getUsersList(Model model, Page page) {
        // 当前页
        page.getCurrentPage();
        List<UsersEntity> usersEntityList = usersService.getUsersList();
        // 赋予总数、总页数
        page.getTotalPage(usersEntityList.size());
        // 起始位置、页大小
        List<UsersEntity> pageList = pageDao.getByLimit(page.getStartPage(), Page.PAGE_SIZE);
        int totalPage = page.getTotalPage();
        model.addAttribute("totals", usersEntityList.size());
        model.addAttribute("data", pageList);
        model.addAttribute("page", page);
        model.addAttribute("totalPage", totalPage);
        return "users";
    }

    // 获取用户权限信息
    @RequestMapping("/userslist2")
    public String getUsersList2(Model model, Page page) {
        page.getCurrentPage();
        List<UsersEntity> usersEntityList = usersService.getUsersList();
        page.getTotalPage(usersEntityList.size());
        List<UsersEntity> pageList = pageDao.getByLimit(page.getStartPage(), Page.PAGE_SIZE);
        int totalPage = page.getTotalPage();
        model.addAttribute("totals", usersEntityList.size());
        model.addAttribute("data", pageList);
        model.addAttribute("page", page);
        model.addAttribute("totalPage", totalPage);
        return "setpermission";
    }

    // 尾页
    @RequestMapping("/getLastPage")
    public String getLastPage(Page page, Model model, @RequestParam("startPage") Integer startPage) { // startpage 10
        List<UsersEntity> usersEntityList = usersService.getUsersList();
        page.getTotalPage(usersEntityList.size());
        model.addAttribute("totals", usersEntityList.size());
        List<UsersEntity> pageList = pageDao.getByLimit(page.getLastPage(), Page.PAGE_SIZE);
        model.addAttribute("data", pageList);
        int totalPage = page.getTotalPage();
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("page", page);
        return "users";
    }

    // 删除用户
    @RequestMapping("/usersdelete")
    @ResponseBody
    public String usersdelete(String id) {
        // 删除前台用户
        feignService2.usersdelete(id);
        usersService.usersdelete(id);
        return "userslist";
    }

    // 修改用户回显
    @RequestMapping("/usersupdateshow")
    @ResponseBody
    public JSONObject usersupdateshow(String id) {
        UsersEntity user = usersService.getUserByUserId(id);
        JSONObject json = (JSONObject) JSONObject.toJSON(user);
        return json;
    }

    // 修改用户
    @RequestMapping("/updateUser")
    @ResponseBody
    public String updateUser(@RequestParam("id") String id, @RequestParam("mobile") String mobile, @RequestParam("fullname") String fullname) {
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
        boolean result = mobile.matches(regex);
        if (!result) {
            return "请输入真实的手机号！";
        } else {
            usersService.updateUser(id, mobile, fullname);
            return "修改成功！";
        }
    }

    // 设置管理员
    @RequestMapping("/setpermission")
    @ResponseBody
    public String setpermission(Integer id) {
        usersService.setpermissionById(id);
        return "/msystem/userslist2";
    }

    // 设置普通用户
    @RequestMapping("/setpermission2")
    @ResponseBody
    public String setpermission2(Integer id) {
        usersService.setpermissionById2(id);
        return "/msystem/userslist2";
    }
}